#include "EditorCheckBoxWidget.h"


#include "Asset/EditorDefaultPackage.h"

#include "Widgets/Mouse/MouseWidget.h"
#include "Render/Widget/RenderWidgets.h"

REditorCheckBoxWidget::REditorCheckBoxWidget()
{
    Mouse=NewWidget<RMouseWidget>(this);
    Mouse->Anchor.Fill(this);
    Mouse->OnClicked.BindLambda([this](const SMouseButtonEvent& Event){
        auto CheckState= this->CheckState.Get();
        switch(CheckState)
        {
            case ECheckBoxWidgetState::Checked:
                OnCheckStateChanged.ExecuteIfBound(ECheckBoxWidgetState::Unchecked);
                break;
            case ECheckBoxWidgetState::Unchecked:
                OnCheckStateChanged.ExecuteIfBound(ECheckBoxWidgetState::Checked);
                break;
            case ECheckBoxWidgetState::Undetermined:
                OnCheckStateChanged.ExecuteIfBound(ECheckBoxWidgetState::Checked);
                break;
        }
        return true;
    });

    //TODO add undetermined icon
    Checked=REditorDefaultPackage::Get()->GetIcon(U"Checked");
    Unchecked=REditorDefaultPackage::Get()->GetIcon(U"Unchecked");
    //Undetermined=REditorDefaultPackage::Get()->GetIcon(U"Undetermined");
    Checked_Highlight=REditorDefaultPackage::Get()->GetIcon(U"Checked_Highlight");
    Unchecked_Highlight=REditorDefaultPackage::Get()->GetIcon(U"Unchecked_Highlight");
    //Undetermined_Highlight=REditorDefaultPackage::Get()->GetIcon(U"Undetermined_Highlight");
}


void REditorCheckBoxWidget::PaintSelf(uint16_t& InZOrder,const SRect& InClipArea)
{
    RTexture2DPtr _Image;
    switch(CheckState.Get())
    {
        case ECheckBoxWidgetState::Checked:
            if(Mouse->IsHovered())
            {
                _Image=Checked_Highlight;
            }
            else
            {
                _Image=Checked;
            }
            break;
        case ECheckBoxWidgetState::Unchecked:
            if(Mouse->IsHovered())
            {
                _Image=Unchecked_Highlight;
            }
            else
            {
                _Image=Unchecked;
            }
            break;
        case ECheckBoxWidgetState::Undetermined:
            if(Mouse->IsHovered())
            {
                _Image=Unchecked_Highlight;
            }
            else
            {
                _Image=Unchecked;
            }
            break;
    }

    if(!_Image)
    {
        return;
    }
    auto RenderImageWidget= std::make_shared< SRenderImageWidget>();
    RenderImageWidget->Image=_Image->GetRenderTexture();
    RenderImageWidget->ScaleMethod=EImageWidgetScaleMethod::Fill;
    RenderImageWidget->Rect=SRect(PresentAbsPosition,PresentSize);
    RenderImageWidget->ClipRect=InClipArea;
    RenderImageWidget->ZOrder=InZOrder;
    InZOrder++;

    PaintRenderWidget(RenderImageWidget);
}
